<template>
  <div class="checkout-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">结算</h1>
      <p class="page-subtitle">确认订单信息并完成支付</p>
    </div>

    <el-row :gutter="30">
      <!-- 左侧：订单信息 -->
      <el-col :span="16">
        <div class="order-section">
          <h2 class="section-title">订单信息</h2>
          
          <!-- 收货地址 -->
          <div class="address-section">
            <h3>收货地址</h3>
            <el-form :model="addressForm" label-width="80px">
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="收货人">
                    <el-input v-model="addressForm.name" placeholder="请输入收货人姓名"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="联系电话">
                    <el-input v-model="addressForm.phone" placeholder="请输入联系电话"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="收货地址">
                    <el-input v-model="addressForm.address" placeholder="请输入详细收货地址"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
          
          <!-- 商品清单 -->
          <div class="products-section">
            <h3>商品清单</h3>
            <el-table :data="cartItems" style="width: 100%">
              <el-table-column label="商品信息">
                <template #default="scope">
                  <div class="product-info">
                    <el-image
                      :src="scope.row.product.coverImage || defaultImage"
                      class="product-image"
                      fit="cover"
                    ></el-image>
                    <div class="product-details">
                      <div class="product-name">{{ scope.row.product.name }}</div>
                      <div class="product-category">{{ scope.row.product.categoryName }}</div>
                    </div>
                  </div>
                </template>
              </el-table-column>
              
              <el-table-column prop="product.price" label="单价" width="120">
                <template #default="scope">
                  <span class="price">¥{{ scope.row.product.price }}</span>
                </template>
              </el-table-column>
              
              <el-table-column prop="quantity" label="数量" width="100">
                <template #default="scope">
                  <span>{{ scope.row.quantity }}</span>
                </template>
              </el-table-column>
              
              <el-table-column label="小计" width="120">
                <template #default="scope">
                  <span class="subtotal">¥{{ (scope.row.product.price * scope.row.quantity).toFixed(2) }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-col>
      
      <!-- 右侧：订单摘要 -->
      <el-col :span="8">
        <div class="summary-section">
          <h2 class="section-title">订单摘要</h2>
          
          <div class="summary-details">
            <div class="summary-item">
              <span>商品总数:</span>
              <span>{{ totalItems }}</span>
            </div>
            <div class="summary-item">
              <span>商品金额:</span>
              <span>¥{{ totalPrice.toFixed(2) }}</span>
            </div>
            <div class="summary-item">
              <span>运费:</span>
              <span>¥0.00</span>
            </div>
            <div class="summary-item">
              <span>优惠券:</span>
              <span>-¥0.00</span>
            </div>
            <div class="summary-divider"></div>
            <div class="summary-item total">
              <span>应付总额:</span>
              <span class="total-price">¥{{ totalPrice.toFixed(2) }}</span>
            </div>
          </div>
          
          <div class="payment-section">
            <h3>支付方式</h3>
            <el-radio-group v-model="paymentMethod">
              <div class="payment-option">
                <el-radio label="alipay" size="large">
                  <div class="payment-label">
                    <i class="el-icon-bank-card"></i>
                    <span>支付宝</span>
                  </div>
                </el-radio>
              </div>
              <div class="payment-option">
                <el-radio label="wechat" size="large">
                  <div class="payment-label">
                    <i class="el-icon-bank-card"></i>
                    <span>微信支付</span>
                  </div>
                </el-radio>
              </div>
              <div class="payment-option">
                <el-radio label="unionpay" size="large">
                  <div class="payment-label">
                    <i class="el-icon-bank-card"></i>
                    <span>银联支付</span>
                  </div>
                </el-radio>
              </div>
            </el-radio-group>
          </div>
          
          <el-button type="primary" size="large" class="submit-btn" @click="submitOrder">
            提交订单并支付
          </el-button>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElRow, ElCol, ElForm, ElFormItem, ElInput, ElTable, ElTableColumn, ElImage, ElRadioGroup, ElRadio, ElButton, ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { getImageUrl } from '../../utils/system'
import useCartStore from '../../stores/cartStore'

const router = useRouter()
const defaultImage = getImageUrl('') // 默认图像
const cartStore = useCartStore()

// 收货地址表单
const addressForm = ref({
  name: '',
  phone: '',
  address: ''
})

// 支付方式
const paymentMethod = ref('alipay')

// 购物车项目
const cartItems = computed(() => cartStore.getCartItems)

// 计算商品总数
const totalItems = computed(() => cartStore.getTotalItems)

// 计算总金额
const totalPrice = computed(() => cartStore.getTotalPrice)

// 提交订单
const submitOrder = () => {
  // 验证表单
  if (!addressForm.value.name || !addressForm.value.phone || !addressForm.value.address) {
    ElMessage.warning('请填写完整的收货地址信息')
    return
  }
  
  // 模拟提交订单
  ElMessage.success('订单提交成功，正在跳转到支付页面...')
  
  // 模拟支付过程
  setTimeout(() => {
    ElMessage.success('支付成功！订单已完成。')
    // 清空购物车并跳转到订单完成页面或首页
    cartStore.clearCart()
    router.push('/front/shop')
  }, 2000)
}

onMounted(() => {
  // 购物车数据现在由状态管理自动处理
})
</script>

<style scoped>
.checkout-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-title {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
}

.page-subtitle {
  font-size: 16px;
  color: #666;
}

.section-title {
  font-size: 20px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.order-section, .summary-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.address-section, .products-section {
  margin-bottom: 30px;
}

.address-section h3, .products-section h3 {
  font-size: 18px;
  margin-bottom: 15px;
}

.product-info {
  display: flex;
  align-items: center;
}

.product-image {
  width: 60px;
  height: 60px;
  border-radius: 6px;
  margin-right: 15px;
}

.product-details {
  flex: 1;
}

.product-name {
  font-weight: bold;
  margin-bottom: 5px;
}

.product-category {
  color: #999;
  font-size: 14px;
}

.price, .subtotal {
  font-weight: bold;
  color: #333;
}

.summary-section {
  position: sticky;
  top: 20px;
}

.summary-details {
  margin-bottom: 30px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.summary-divider {
  height: 1px;
  background: #eee;
  margin: 15px 0;
}

.total {
  font-size: 18px;
  font-weight: bold;
}

.total-price {
  color: #ff6600;
  font-size: 20px;
}

.payment-section h3 {
  font-size: 18px;
  margin-bottom: 15px;
}

.payment-option {
  margin-bottom: 15px;
  border: 1px solid #eee;
  border-radius: 6px;
  padding: 10px;
}

.payment-label {
  display: flex;
  align-items: center;
  gap: 10px;
}

.submit-btn {
  width: 100%;
  margin-top: 20px;
}
</style>